import API from "@/api";
import { Router } from '@/common/router';

// 页面跳转枚举
export enum CoursePageEnum {
    LISTEN,
    GAP,
    RESULT
}

// 随课自检数据
export const Course = {
    sectionId: '',
    source: [] as any[],
    currentRouter: "" as string,
    queryParams: {
        totalProgress: 0,
        time: 0
    },
    data: {
        [CoursePageEnum.GAP]: {
            sourceUrl: Router.Home.selfGapFilling,
            content_id: 0
        },
        [CoursePageEnum.LISTEN]: {
            sourceUrl: Router.Home.selfListeningExercise,
            content_id: 0
        },
        [CoursePageEnum.RESULT]: {
            sourceUrl: Router.Home.selfInspectionResult
        }
    },
    async init(section_id: any, callback: Function) {
        this.sectionId = section_id;
        try {
            const selfTestCourse: any = await API.getSelfTestCourseList({ section_id });
            this.currentRouter = "";
            this.queryParams.totalProgress = 0;
            this.queryParams.time = 0;
            this.source = selfTestCourse?.answerSort || [];
            for (let i = 0; i < this.source.length; i++) {
                switch (this.source[i].type) {
                    case CoursePageEnum.LISTEN:
                        this.data[CoursePageEnum.LISTEN].content_id = this.source[i].content_id;
                        break;
                    case CoursePageEnum.GAP:
                        this.data[CoursePageEnum.GAP].content_id = this.source[i].content_id;
                        break;
                }
                if (!this.source[i].is_complete && !this.currentRouter) {
                    this.queryParams.time = selfTestCourse.use_time;
                    this.currentRouter = this.data[this.source[i].type as CoursePageEnum].sourceUrl;
                }
                this.queryParams.totalProgress += this.source[i].topic_num;
            }
            !this.currentRouter && (this.currentRouter = this.data[CoursePageEnum.RESULT].sourceUrl);
            callback && callback();
        } catch (e) {
            console.error(e);
        }
    }
}